﻿<%@ WebHandler Language="C#" Class="AnalysisHandler" %>

using System;
using System.Web;
using System.Collections.Generic;

using EHSubmit.Logic;
using EHSubmit.Logic.Analysis;
using EHSubmit.Model;
using EHSubmit.Util;

public class AnalysisHandler : BaseAjaxHandler {
    protected override void Mapping()
    {
        OprationMap["course"] = "CourseAnalysisScores";         // 课程分数分析数据
        OprationMap["student"] = "StudentAverageScore";         // 学生课程作业平均分数据
        OprationMap["goal"] = "GoalAnalysisScores";             // 某次作业成绩分布饼图
        OprationMap["LevelConfirm"] = "ConfirmScoreLevel";      // 确认学生成绩所在的级别
    }

    /// <summary>
    /// 课程平均分走势图
    /// </summary>
    public void CourseAnalysisScores()
    {
        if (Validate(ERole.Teacher))
        {
            string courseId = Context.Request["courseId"];


            IList<GoalScore> scores = new ScoreManager().GetCourseScores(courseId);
            
            string jsonData = JsonHelper.Serialize(scores);
            
            IsSuccess = true;
            ResponseText = "{" + string.Format("data:{0}", jsonData) + "}";
        }
        else if (Validate(ERole.Student))
        {
            int i,j = 0;
            bool myScoresIsEnd = false;
            string format = "GoalID:{0},GoalName:\"{1}\",Max:{2},Min:{3},Average:{4},myScore:{5}";
            string jsonData = null;
            string courseID = Context.Request["courseID"];

            ScoreManager scoreManager = new ScoreManager();
            GoalScore classScore = new GoalScore();
            HomeworkScore myScore = new HomeworkScore();
            
            //---返回的Scores都已按goalid排序
            IList<GoalScore> classScores = scoreManager.GetCourseScores(courseID);
            IList<HomeworkScore> myScores = scoreManager.GetScoresOfCourseByStu(CurrentUser.ID, courseID);
            
            //---注意对作业未交的，未评分的识别处理，剩下的要与classScores对应
            //---有班级成绩未必有我的成绩，有我的成绩必有班级成绩 classScores数量>=myScores数量
            for (i = 0; i < classScores.Count; i++)
            {
                classScore = classScores[i];
                //---判断是否我的成绩都有交，一下处理未交的情况
                if (j != myScores.Count)
                {
                    myScore = myScores[j];
                    myScoresIsEnd = false;
                }
                else
                {
                    myScoresIsEnd = true;
                }

                //---未有作业缺交-------
                if (!myScoresIsEnd && classScore.GoalID == myScore.GoalID)
                {
                    jsonData += "{" + string.Format(format, classScore.GoalID, classScore.GoalName, classScore.Max, classScore.Min, classScore.Average, myScore.Score) + "}";
                }
                else
                {
                    jsonData += "{" + string.Format(format, classScore.GoalID, classScore.GoalName, classScore.Max, classScore.Min, classScore.Average, "{}") + "}";
                    j--;
                }
                j++;
                if (i != classScores.Count - 1)
                {
                    jsonData = jsonData + ',';
                }
            }


            /*数据最终如下：
             *{data:[{GoalID:1,GoalName:"Title0",Max:90,Min:65,Average:76,myScore:65},
             * {GoalID:4,GoalName:"Title3",Max:78,Min:64,Average:69,myScore:{}},   比如该次作业有交，但尚未评分，形如左
             * {GoalID:5,GoalName:"Title4",Max:92,Min:66,Average:79,myScore:85},
             * {GoalID:6,GoalName:"Title5",Max:92,Min:76,Average:84,myScore:{}}]}  比如该次作业未交，形如左*/
            IsSuccess = true;
            ResponseText = "{" + string.Format("data:[{0}]", jsonData) + "}";

        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// 课程>>学生平均成绩表
    /// </summary>
    public void StudentAverageScore()
    {
        if (Validate(ERole.Teacher))
        {
            string corseId = Context.Request["courseId"];


            IList<StudentScore> scores = new ScoreManager().GetStudentAverageScore(corseId);

            string jsonData = JsonHelper.Serialize(scores);

            IsSuccess = true;
            ResponseText = "{" + string.Format("data:{0}", jsonData) + "}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }
    
    /// <summary>
    /// 某作业饼图数据
    /// </summary>
    public void GoalAnalysisScores()
    {
        if (Validate(ERole.Teacher) || Validate(ERole.Student))
        {
            int goalId = int.Parse(Context.Request["goalId"]);

            ChartDataDigester digester = new ChartDataDigester();

            string data = JsonHelper.Serialize(digester.GetPieChartData(goalId));

            IsSuccess = true;
            ResponseText = "{" + string.Format("data:{0}",data)+"}";
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// 确认学生成绩所在的级别
    /// </summary>
    public void ConfirmScoreLevel()
    {
        if(Validate(ERole.Student))
        {
            int goalID = WebHelper.IntSafeConvert(Context.Request["goalID"],-1);
            if (goalID != -1)
            {
                //'您未交该作业，您上交的该作业尚未评分，您的分数为**，处在**分数段，（鼓励的话）';
                string reply = null;
                ScoreManager scoreManager = new ScoreManager();
                HomeworkScore hwScore = scoreManager.GetScoreOfGoal(CurrentUser.ID, goalID);
                if (hwScore == null)
                {
                    reply = "您未交该作业!";
                }
                else if (hwScore.Score == -1)
                {
                    reply = "您上交的该作业尚未评分!";
                }
                else
                {
                    if (hwScore.Score <= 60)
                    {
                        reply = ",为60分以下(包括60),请下次努力啊!";
                    }
                    else if (hwScore.Score <= 80)
                    {
                        reply = ",处在60至80分之间(不包括60,包括80),还不够哦!";
                    }
                    else if (hwScore.Score <= 90)
                    {
                        reply = ",处在80至90分之间(不包括80,包括90),再加把劲哦!";
                    }
                    else
                    {
                        reply = ",为90分以上(不包括90),请再接再厉!";
                    }
                    reply = "您的分数为" + hwScore.Score.ToString() + reply;
                }

                IsSuccess = true;
                ResponseText = "{" + string.Format("success:true,reply:\"{0}\"", reply) + "}";
            }
            else
            {
                Message = Constants.INFO_UNKNOWN_ERR;
            }
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }
    

}